package com.zmc.admin.controller.system;

import com.zmc.admin.infrastructure.bean.vo.dict.SysDictTypeVo;
import com.zmc.admin.infrastructure.entity.SysDictTypeEntity;
import com.zmc.admin.infrastructure.bean.query.SysDictTypeQuery;
import com.zmc.admin.infrastructure.service.SysDictTypeService;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 数据字典信息
 * @author zmc
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController {
    @Autowired
    private SysDictTypeService dictTypeService;

    @Autowired
    private AuthTokenService authTokenService;


    @PostMapping("/list")
    public R list(@RequestBody SysDictTypeQuery query) {
        PageUtils page = dictTypeService.queryPage(query);
        return R.page(page);
    }

    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, SysDictTypeQuery query) {
        List<SysDictTypeEntity> list = dictTypeService.selectDictTypeList(query);
        ExcelUtil<SysDictTypeEntity> util = new ExcelUtil<>(SysDictTypeEntity.class);
        util.exportExcel(response, list, "字典类型");
    }


    @PostMapping(value = "/info")
    public R getInfo(@RequestBody SysDictTypeVo vo) {
        return R.ok(dictTypeService.getById(vo.getDictId()));
    }

    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysDictTypeEntity dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(authTokenService.getUserInfo().getUsername());
        boolean flag = dictTypeService.save(dict);
        return flag ? R.ok(): R.error();
    }

    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@Validated @RequestBody SysDictTypeEntity dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(authTokenService.getUserInfo().getUsername());
        boolean flag = dictTypeService.updateById(dict);
        return flag ? R.ok(): R.error();
    }

    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody SysDictTypeVo vo) {
        dictTypeService.deleteDictTypeById(vo.getDictId());
        return R.ok();
    }

    @PostMapping("/refreshCache")
    public R refreshCache() {
        dictTypeService.resetDictCache();
        return R.ok();
    }

    @PostMapping("/optionselect")
    public R optionSelect() {
        List<SysDictTypeEntity> dictTypes = dictTypeService.list();
        return R.ok(dictTypes);
    }
}
